﻿@page
@using Projects.OssDownload.Pages
@model IndexModel
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>OSS下载代理</title>
    <link rel="stylesheet" href="/css/site.css" asp-append-version="true" />
</head>
<body>
    <div class="container">
        <main role="main" class="pb-3">
            <div class="text-center mt-5">
                <h1 class="display-4">OSS下载代理</h1>
                <form id="downloadForm">
                    <div class="form-group">
                        <label for="remoteUrl">请输入远程URL:</label>
                        <input type="text" class="form-control" id="remoteUrl" placeholder="https://example.com/file.zip" />
                    </div>
                    <button type="button" class="btn btn-primary" onclick="generateProxyLink()">生成代理下载链接</button>
                </form>

                <div class="mt-3" id="proxyLinkContainer" style="display: none;">
                    <p>代理下载链接: <a id="proxyDownloadLink" href="#" target="_blank"></a></p>
                    <button type="button" class="btn btn-secondary" onclick="copyToClipboard()">复制链接</button>
                </div>
            </div>
        </main>
    </div>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function generateProxyLink() {
            const remoteUrlInput = document.getElementById('remoteUrl');
            const remoteUrl = remoteUrlInput.value.trim();

            if (!remoteUrl || !remoteUrl.startsWith('https://')) {
                alert('请输入有效的HTTPS URL.');
                return;
            }

            const baseUrl = window.location.origin + '/dl/';
            const encodedPath = remoteUrl.replace('https://', '');
            const proxyDownloadLink = baseUrl + encodedPath;

            const proxyLinkContainer = document.getElementById('proxyLinkContainer');
            const proxyDownloadLinkElement = document.getElementById('proxyDownloadLink');

            proxyDownloadLinkElement.href = proxyDownloadLink;
            proxyDownloadLinkElement.textContent = proxyDownloadLink;
            proxyLinkContainer.style.display = 'block';
        }

        function copyToClipboard() {
            const proxyDownloadLinkElement = document.getElementById('proxyDownloadLink');
            const tempInput = document.createElement('input');
            tempInput.value = proxyDownloadLinkElement.href;
            document.body.appendChild(tempInput);
            tempInput.select();
            document.execCommand('copy');
            document.body.removeChild(tempInput);
            alert('链接已复制到剪贴板!');
        }
    </script>
</body>
</html>